class MyApp {
    public constructor() {
        App.ResourceUtils.loadGroup("gameui", this.onResourceLoadComplete, this.onResourceLoadProgress, this)
    }

    private onResourceLoadComplete(param1: any, param2: any): void {
        if (param1 == "gameui") {
            App.SceneManager.runScene(SceneConsts.GAME);


            App.ControllerManager.applyFunc(ControllerConsts.Game, GameUIConsts.SetPkProgress, 10, 110)
            App.ControllerManager.applyFunc(ControllerConsts.Game, GameUIConsts.SetGameInfo, "测试的比赛")
            App.ControllerManager.applyFunc(ControllerConsts.Game, GameUIConsts.SetGame, AllGameConsts.Calculate)

            protobuf.load("resource/proto/example.proto", function (err, root): void {
                console.log("1111111111" + err);
                if (err == null) {
                    var AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");
                    var buffer: Uint8Array;
                    {
                        var payload = { awesomeField: "AwesomeString ====== 1111111111111" };
                        var message = AwesomeMessage.create(payload);
                        buffer = AwesomeMessage.encode(message).finish();
                    }
                    {
                        var message = AwesomeMessage.decode(buffer);
                        var object = AwesomeMessage.toObject(message, {
                            longs: String,
                            enums: String,
                            bytes: String,
                            // see ConversionOptions
                        });
                        console.log(object);
                    }
                }
            })
        }

        egret.MainContext.instance.stage.addEventListener(egret.Event.ENTER_FRAME, this.testProtoJS, this);
    }

    private testProtoJS(): void {
        //egret.MainContext.instance.stage.removeEventListener(egret.Event.ENTER_FRAME, this.testProtoJS, this);

        var protoSource: string = `
syntax = "proto3";

package awesomepackage;

message AwesomeMessage {
    string awesome_field = 1; // becomes awesomeField
    uint64 number_field = 2;
}`;
        var root: protobuf.Root = new protobuf.Root();
        protobuf.parse(protoSource, root);
        var AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");
        
        for (var i = 0; i < 10; i++) {
            var buffer: Uint8Array;
            {
                var payload = { awesomeField: "AwesomeString1111111111111", numberField: 100 };
                var message = AwesomeMessage.create(payload);
                buffer = AwesomeMessage.encode(message).finish();
            }
            {
                var message = AwesomeMessage.decode(buffer);
                var object = AwesomeMessage.toObject(message, null);
                //console.log(object);
            }
        }
    }


    private onResourceLoadProgress(itemsLoaded: number, itemsTotal: number): void {
        App.ControllerManager.applyFunc(ControllerConsts.Loading, LoadingConst.SetProgress, itemsLoaded, itemsTotal);
    }
}